//创建audio控件
const myaudio = wx.getBackgroundAudioManager();
const recorderManager = wx.getRecorderManager();
const innerAudioContext = wx.createInnerAudioContext()
var tempFilePath;
var Toast = require('../../utils/toast.js');
Page({
  data: {
    recordtype: 0,
    tabIndex: 0,
    //音频列表
    audioArr: [{
      id: '000',
      title: '爆袭全站欢快节奏感觉那是杠杠滴',
      src: 'http://mp3.djwma.com/mp3/爆袭全站欢快节奏感觉那是杠杠滴.mp3',
      time: '30s',
      bl: false
    },
    {
      id: '001',
      title: '好听的欧美男声 网络流行',
      src: 'http://mp3.djwma.com/mp3/好听的欧美男声 网络流行.mp3',
      time: '50s',
      bl: false
    }
    ]
  },
  onLoad: function () { },
  changeTab(e) {
    let tabIndex = e.currentTarget.dataset.id
    this.setData({
      tabIndex: tabIndex
    })
  },
  //音频播放  
  audioPlay: function (e) {
    let that = this,
      id = e.currentTarget.dataset.id,
      key = e.currentTarget.dataset.key,
      audioArr = that.data.audioArr,
      vidSrc = audioArr[key].src,
      vTitie = audioArr[key].title;
    myaudio.src = vidSrc;
    myaudio.title = vTitie;

    //切换显示状态
    for (var i = 0; i < audioArr.length; i++) {
      audioArr[i].bl = false;
    }
    console.log(key)
    audioArr[key].bl = true;
    //开始监听
    myaudio.onPlay(() => {
      that.setData({
        audioArr: audioArr
      })
    })
    //结束监听
    myaudio.onEnded(() => {
      audioArr[key].bl = false;
      that.setData({
        audioArr: audioArr
      })
    })
  },

  // 音频停止
  audioStop: function (e) {
    let that = this,
      key = e.currentTarget.dataset.key,
      audioArr = that.data.audioArr;
    //切换显示状态
    for (var i = 0; i < audioArr.length; i++) {
      audioArr[i].bl = false;
    }
    audioArr[key].bl = false;
    myaudio.stop();
    //停止监听
    myaudio.onStop(() => {
      console.log(5345345)
      audioArr[key].bl = false;
      that.setData({
        audioArr: audioArr,
      })
    })
    //结束监听
    myaudio.onEnded(() => {
      console.log(234324)
      audioArr[key].bl = false;
      that.setData({
        audioArr: audioArr,
      })
    })
  },
  //开始录音的时候
  start: function () {
    const options = {
      duration: 10000, //指定录音的时长，单位 ms
      sampleRate: 44100, //采样率
      numberOfChannels: 1, //录音通道数
      encodeBitRate: 192000, //编码码率
      format: 'aac', //音频格式，有效值 aac/mp3
      frameSize: 50, //指定帧大小，单位 KB
      duration: 10000
    }
    //开始录音
    this.setData({
      recordtype: 1
    })
    recorderManager.onStart(() => {
      console.log('recorder start')
      Toast('录音开始中', 'success', 1000)
    });
    recorderManager.start(options);
    //错误回调
    recorderManager.onError((res) => {
      console.log(res);
    })
  },
  //停止录音
  stop: function () {
    recorderManager.stop();
    //开始录音
    this.setData({
      recordtype: 0
    })
    recorderManager.onStop((res) => {
      this.tempFilePath = res.tempFilePath;
      console.log('停止录音', res.tempFilePath)
      Toast('停止录音', 'success', 1000)
      const {
        tempFilePath
      } = res
      console.log(this.data.audioArr.length)
      let audioArrlength = this.data.audioArr.length
      let audioArrinfo = {
        id: '00' + parseInt(audioArrlength + 1),
        title: '录音',
        src: res.tempFilePath,
        time: '50s',
        bl: false
      }
      console.log(audioArrinfo)
      this.setData({
        audioArr: this.data.audioArr.push(audioArrinfo)
      })
    })
  },
  //播放声音
  play: function () {
    innerAudioContext.autoplay = true
    innerAudioContext.src = this.tempFilePath,
      innerAudioContext.onPlay(() => {
        console.log('开始播放')
        Toast('开始播放', 'success', 1000)
      })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })

  }
})